﻿using CXW.Core.Sharp.Services;
using DevExpress.XtraTreeList;
using DevExpress.XtraTreeList.Columns;
using DevExpress.XtraTreeList.Nodes;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Windows.Forms;

namespace CXW.Core.Sharp.Gui
{
    class TreeListExt : TreeList
    {
        List<TreeListNode> _nodes = new List<TreeListNode>();
        object owner = null;
        public TreeListExt()
        {
            Dock = DockStyle.Fill;
            OptionsBehavior.Editable = false;
            OptionsView.ShowHorzLines = false;
            OptionsView.ShowVertLines = false;
            OptionsView.ShowIndicator = false;
            OptionsView.ShowColumns = false;

            Columns.Add(new TreeListColumn() { Visible = true });

            BeforeExpand += (sender, e) => { if (e.Node != null) { UpdateText(e.Node.Nodes); } };

            MouseDoubleClick += (sender, e) =>
            {
                if (e.Button == MouseButtons.Left)
                {
                    TreeListHitInfo hi = CalcHitInfo(e.Location);
                    if (hi != null && hi.Node != null && hi.Node.Nodes.Count == 0 && !_nodes.Contains(hi.Node))
                    {//只有纯子节点才有双击节点事件处理 
                        ICommand cmd = CommandWrapper.CreateCommand(FocusedNode.Tag as Codon, owner);
                        if (cmd == null) { _nodes.Add(hi.Node); this.Invalidate(); }
                        else { cmd.Run(); }
                    }
                }
            };

            CustomDrawNodeCell += (sender, e) =>
            {
                if (_nodes.Contains(e.Node)) { e.Appearance.ForeColor = System.Drawing.Color.Gray; }
            };
        }

        public TreeListExt(IList Items) : this()
        {
            CreateTreeListNode(Items);
        }
        public TreeListExt(IList Items, object owner) : this(Items)
        {
            this.owner = owner;
        }

        void CreateTreeListNode(IList subItems, TreeListNode parentNode = null)
        {
            foreach (IItemDescriptor des in subItems)
            {
                CreateTreeListNode(des.SubItems, CreatedNode(des.Codon, null));
            }
        }

        public TreeListNode CreatedNode(Codon codon, TreeListNode parentNode)
        {
            string type = codon.Properties.Contains("type") ? codon.Properties["type"] : "TreeNode";
            if (type != "TreeNode")
            {
                throw new NotSupportedException("unsupported TreeItem type : " + type);
            }

            return AppendNode(new object[] { LanguageHelper.GetText(codon) }, parentNode, codon);
        }


        public static void UpdateText(TreeListNodes nodes)
        {
            foreach (TreeListNode node in nodes)
            {
                node.SetValue(0, LanguageHelper.GetText(node.Tag as Codon));
                if (node.Expanded)
                {
                    UpdateText(node.Nodes);
                }
            }
        }
    }
}
